package com.bf.electroplating.controller;

import com.bf.electroplating.service.ICompanyDataSyncService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 企业数据同步控制器
 * 
 * @author gzd
 * @since 2025-09-22
 */
@Slf4j
@RestController
@RequestMapping("/api/company/sync")
@Tag(name = "企业数据同步", description = "企业数据同步相关接口")
public class CompanyDataSyncController {

    @Autowired
    private ICompanyDataSyncService companyDataSyncService;

    /**
     * 同步企业列表
     */
    @PostMapping("/company/list")
    @Operation(summary = "同步企业列表", description = "从外部接口同步企业列表数据")
    public String syncCompanyList() {
        try {
            companyDataSyncService.syncCompanyList();
            return "企业列表同步成功";
        } catch (Exception e) {
            log.error("同步企业列表异常", e);
            return "企业列表同步失败：" + e.getMessage();
        }
    }

    /**
     * 同步企业设备列表
     */
    @PostMapping("/company/device/list")
    @Operation(summary = "同步企业设备列表", description = "根据企业ID同步设备列表数据")
    public String syncCompanyDeviceList(@Parameter(description = "企业ID") @RequestParam String companyId) {
        try {
//            companyDataSyncService.syncCompanyDeviceList(companyId);
            return "企业设备列表同步成功";
        } catch (Exception e) {
            log.error("同步企业设备列表异常，企业ID：{}", companyId, e);
            return "企业设备列表同步失败：" + e.getMessage();
        }
    }

    /**
     * 同步企业设备状态
     */
    @PostMapping("/company/device/status")
    @Operation(summary = "同步企业设备状态", description = "根据设备ID和日期同步设备状态数据")
    public String syncCompanyDeviceStatus(@Parameter(description = "设备ID") @RequestParam String deviceId,
                                        @Parameter(description = "日期，格式：yyyy-MM-dd") @RequestParam String curDate) {
        try {
            companyDataSyncService.syncCompanyDeviceStatus(deviceId, curDate);
            return "企业设备状态同步成功";
        } catch (Exception e) {
            log.error("同步企业设备状态异常，设备ID：{}，日期：{}", deviceId, curDate, e);
            return "企业设备状态同步失败：" + e.getMessage();
        }
    }

    /**
     * 同步所有企业数据
     */
    @PostMapping("/all")
    @Operation(summary = "同步所有企业数据", description = "同步企业列表、设备列表和设备状态数据")
    public String syncAllCompanyData() {
        try {
            companyDataSyncService.syncAllCompanyData();
            return "所有企业数据同步成功";
        } catch (Exception e) {
            log.error("同步所有企业数据异常", e);
            return "所有企业数据同步失败：" + e.getMessage();
        }
    }
}
